package server

import (
	"errors"
	"short-video-media/common/models/mysql"
	"short-video-media/common/models/redis"
	work "short-video-media/common/proto"
)

func AddWork(in *work.AddWorkReq) (*work.AddWorkResp, error) {
	w := &mysql.Works{}
	err := w.AddWork(in)
	if err != nil {
		return nil, errors.New("作品信息添加失败")
	}
	return &work.AddWorkResp{Greet: "添加成功"}, nil
}
func UpdateWork(in *work.UpdateWorkReq) (*work.UpdateWorkResp, error) {
	w := &mysql.Works{}
	err := w.UpdateWork(in)
	if err != nil {
		return nil, errors.New("修改失败")
	}
	return &work.UpdateWorkResp{Greet: "修改成功"}, nil
}

func FindWork(in *work.FindWorkReq) (*work.FindWorkResp, error) {
	w := &mysql.Works{}
	findWork, err := w.FindWork(in.KeyWord)
	if err != nil {
		return nil, errors.New("查询失败")
	}
	var Items []*work.FindWork
	for _, m := range findWork {
		Items = append(Items, &work.FindWork{
			WorksName:    m.WorksName,
			WorksContent: m.WorksContent,
			WorksType:    m.WorksType,
			WorksUrl:     m.WorksUrl,
			TimeCount:    m.TimeCount,
		})
		redis.WorkToRedis("WorkInfo", m)
	}

	return &work.FindWorkResp{List: Items}, nil
}
